﻿using System;
using UnityEngine;


[DefaultExecutionOrder(-101)]
public class UILayerCollect : MonoBehaviour
{
    public bool hideOnStart = false;
    public GameObject[] mPrefabList;

    [Tooltip("游戏开始时保持打开的界面状态")]
    public GameObject[] mEnterList;

    public Transform trsRoot;

    private void Awake()
    {

        if (hideOnStart)
        {
            //关闭所有界面
            foreach (Transform trans in this.transform)
            {
                if (Array.IndexOf(mEnterList, trans.gameObject) == -1)
                {
                    trans.gameObject.SetActive(false);
                }
                else
                {
                    trans.gameObject.SetActive(true);
                }
            }
        }
    }


    public GameObject GetFromPrefab<T>()
    {
        for (int i = 0; i < this.mPrefabList.Length; i++)
        {
            var comp = this.mPrefabList[i].GetComponent<T>();
            if (comp != null)
            {
                return this.mPrefabList[i];
            }
        }
        return null;
    }

    /// <summary>
    /// 获取一个弹窗界面，如果UIManager 下找不到，那从 prefabList里查找，然后捞出来
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T GetWindow<T>(bool autoActive = true) where T : Component
    {
        T selectComp = default(T);
        //反射激活类

        string compName = typeof(T).Name;

        //从缓存中读取弹窗界面
        foreach (Transform trans in this.transform)
        {
            T comp = trans.GetComponent<T>();
            if (comp != null)
            {
                GameObject obj = comp.gameObject;
                //从缓存中激活使用
                if (obj != null)
                {
                    obj.SetActive(autoActive);
                    selectComp = comp;
                    break;
                }
            }
        }

        //重新创建一个新的界面
        if (selectComp == null)
        {
            // print(compName + "IS NULL");
            var prefab = this.GetFromPrefab<T>();
            if (prefab != null)
            {
                var myGameObject = Instantiate(prefab);
                myGameObject.transform.SetParent(this.transform);
                myGameObject.transform.localScale = Vector3.one;
                myGameObject.transform.localEulerAngles = Vector3.zero;
                myGameObject.transform.localPosition = Vector3.zero;
                var rect = myGameObject.GetComponent<RectTransform>();
                if (rect)
                {
                    rect.offsetMin = new Vector2(0, 0);
                    rect.offsetMax = new Vector2(0, 0);
                }

                myGameObject.SetActive(autoActive);
                selectComp = myGameObject.GetComponent<T>();//获取T
            }
        }

        return selectComp;

    }

    public GameObject GetTargetGameObject<T>() where T : Component
    {
        var window = this.GetComponentInChildren<T>();
        if (window == null) return null;
        return window.gameObject;
    }

    public GameObject GetWindowByName (string name) {
        //从缓存中读取弹窗界面
        foreach (Transform trans in this.transform)
        {
            if (trans.name == name)
            {
                return trans.gameObject;
            }
        }     
        return null;
    }


    /// <summary>
    /// 打开一个弹窗
    /// </summary>
    /// <param name="openToTop"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T OpenWindow<T>() where T : Component
    {
        return this.GetWindow<T>(true);
    }

}

